package com.junbao.message.infrastructure.core.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.junbao.commom.context.ClassPathXmlApplicationContext;
import com.junbao.message.application.service.MessageService;
import com.junbao.message.domain.model.NotifyMessage;
import com.junbao.message.infrastructure.cache.RedisService;
import com.junbao.message.infrastructure.cache.key.MessageKey;
import com.junbao.message.infrastructure.core.DataConsumer;
import com.junbao.message.infrastructure.core.RequestDto;

/**
 * 消息发送消费者
 * @author yilin
 *
 */
public class MessageSendHandler extends DataConsumer<RequestDto<NotifyMessage>>{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(MessageSendHandler.class);
	
	private MessageService messageService = (MessageService) ClassPathXmlApplicationContext.getInstance().getBean("messageService");
	
	@Override
	public void onEvent(RequestDto<NotifyMessage> event) throws Exception {
		NotifyMessage notifyMessage = event.getBusinessObject();
		try {
			LOGGER.info(Thread.currentThread().getName()+" 序列号： "+notifyMessage.getMessageSquece()+" 执行消息发送到目标系统");
			messageService.sendMessage(notifyMessage);
			RedisService redisService = RedisService.getInstance();
			redisService.set(MessageKey.MESSAGE_MESSAGE_NO, notifyMessage.getMessageNo(), notifyMessage);
		} catch (Exception e) {
			//LOGGER.info("消息发送失败！消息详情=>"+notifyMessage.toString());
			LOGGER.error("消息编号： "+notifyMessage.getMessageNo(), e);
		}
	}
}
